package com.czz.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.common.exception.CustomerException;
import com.czz.common.exception.NotFoundException;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.portal.entity.Chapter;
import com.czz.portal.entity.Course;
import com.czz.portal.entity.Period;
import com.czz.portal.mapper.ChapterMapper;
import com.czz.portal.mapper.CourseMapper;
import com.czz.portal.mapper.PeriodMapper;
import com.czz.portal.query.CourseQuery;
import com.czz.portal.service.CourseService;
import com.czz.portal.vo.ChapterVo;
import com.czz.portal.vo.CourseVo;
import com.czz.portal.vo.PeriodVo;
import com.czz.portal.vo.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CourseServiceImpl
 * @Author Czz
 * @Date 2020/2/6
 * @Time 13:22
 **/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    ChapterMapper chapterMapper;

    @Autowired
    PeriodMapper periodMapper;


    @Override
    public List<Course> getRecommendCourseList() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_status",0);
        queryWrapper.orderByDesc("course_buy_sum");
        queryWrapper.last("limit 0,5");
        List courseList = courseMapper.selectList(queryWrapper);
        return courseList;
    }

    @Override
    public List<Course> getNewCourseList() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_status",0);
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.last("limit 0,10");
        List courseList = courseMapper.selectList(queryWrapper);
        return courseList;
    }

    @Override
    public List<Course> getGuideCourseList() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_status",0);
        queryWrapper.eq("course_level",0).or().eq("course_level",1);
        queryWrapper.last("order by RAND() limit 0,10");
        List courseList = courseMapper.selectList(queryWrapper);
        return courseList;
    }

    @Override
    public List<Course> getImproveCourseList() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_status",0);
        queryWrapper.eq("course_level",2).or().eq("course_level",3);
        queryWrapper.last("order by RAND() limit 0,10");
        List courseList = courseMapper.selectList(queryWrapper);
        return courseList;
    }

    @Override
    public List<Course> getTechnologyCourseList() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_status",0);
        queryWrapper.eq("category_parent_id","1215453797468356610").or().eq("category_parent_id","1215453797208309761");
        queryWrapper.last("order by RAND() limit 0,10");
        List courseList = courseMapper.selectList(queryWrapper);
        return courseList;
    }

    @Override
    public CourseVo getCourseDetailById(String id) {

        //填充CourseVo
        Course course = courseMapper.selectById(id);
        if (ObjectUtils.isEmpty(course)){
            throw new CustomerException(ResultCodeEnum.NOT_FOUND);
        }

        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course,courseVo);

        //填充CourseVo中ChapterVo
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",id);
        chapterQueryWrapper.orderByDesc("chapter_sort");
        List<Chapter> chapterList = chapterMapper.selectList(chapterQueryWrapper);

        List<ChapterVo> chapterVoList = new ArrayList<>();

        for (int i = 0; i < chapterList.size(); i++){
            //chapter 赋值给 chapterVo
            Chapter chapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoList.add(chapterVo);

            //赋值chapterVo中List<period>
            QueryWrapper<Period> periodQueryWrapper = new QueryWrapper<>();
            periodQueryWrapper.eq("chapter_id",chapter.getChapterId());
            periodQueryWrapper.orderByDesc("period_sort");
            List<Period> periodList = periodMapper.selectList(periodQueryWrapper);
            chapterVo.setPeriod(periodList);

        }
        courseVo.setChapter(chapterVoList);

        return courseVo;
    }

    @Override
    public List<CourseVo> getRecommendCourseListById(String id) {
        Course course = courseMapper.selectById(id);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_status",0);
        queryWrapper.eq("category_parent_id",course.getCategoryParentId());
        queryWrapper.last("order by RAND() limit 0,5");
        List<Course> courseList = courseMapper.selectList(queryWrapper);
        List<CourseVo> courseVoList = new ArrayList<>();

        for (int i = 0; i < courseList.size(); i++){
            CourseVo courseVo = new CourseVo();
            Course temp = courseList.get(i);
            BeanUtils.copyProperties(temp, courseVo);
            courseVoList.add(courseVo);
        }

        return courseVoList;
    }

    @Override
    public void getCoursePageList(Page<Course> pageParam, CourseQuery courseQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_status",0);

        if (ObjectUtils.isEmpty(courseQuery)){
            courseMapper.selectPage(pageParam,queryWrapper);
            return;
        }

        String name = courseQuery.getName();
        String sort = courseQuery.getSort();
        String parent = courseQuery.getParent();
        String category = courseQuery.getCategory();
        Integer level = courseQuery.getLevel();
        Integer price = courseQuery.getPrice();


        //课程名匹配
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("course_name",name);
        }

        //课程一级分类匹配
        if (!StringUtils.isEmpty(parent)){
            queryWrapper.eq("category_parent_id",parent);
        }

        //课程二级分类匹配
        if (!StringUtils.isEmpty(category)){
            queryWrapper.eq("category_id",category);
        }

        //课程等级匹配
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("course_level",level);
        }

        //是否免费匹配 1全部，0免费
        if (price.equals(0)){
            queryWrapper.eq("course_price",0.00);
        }

        if (price.equals(1)){
            queryWrapper.ne("course_price",0.00);
        }

        //排序匹配 normal默认,new最新,hot最热
        if (!StringUtils.isEmpty(sort)){
            if (sort.equals("new")){
                queryWrapper.orderByDesc("gmt_create");
            }
            if (sort.equals("hot")){
                queryWrapper.orderByDesc("course_buy_sum");
            }
        }

        courseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public Period getPeriodByPeriodId(String id) {
        Period period = periodMapper.selectById(id);
        return period;
    }

    @Override
    public Boolean getCourseIsFree(String id) {
        Chapter chapter = chapterMapper.selectById(id);
        Course course = courseMapper.selectById(chapter.getCourseId());
        if (course.getPrice().compareTo(BigDecimal.ZERO) == 0){
            return true;
        }
        return false;
    }

    @Override
    public PeriodVo getPeriodByVideoId(String id) {
        QueryWrapper<Period> queryWrapper = new QueryWrapper();
        queryWrapper.eq("video_source_id",id);
        Period period = periodMapper.selectOne(queryWrapper);
        Course course = courseMapper.selectById(period.getCourseId());
        Chapter chapter = chapterMapper.selectById(period.getChapterId());

        PeriodVo periodVo = new PeriodVo();
        periodVo.setPeriodId(period.getPeriodId());
        periodVo.setPeriodName(period.getPeriodName());
        periodVo.setCourseId(period.getCourseId());
        periodVo.setCourseName(course.getName());
        periodVo.setChapterName(chapter.getName());

        if (period.getFree().equals(1)){
            periodVo.setPeriodFree(true);
        }
        else {
            periodVo.setPeriodFree(false);
        }


        if (course.getPrice().compareTo(BigDecimal.ZERO) == 0){
            periodVo.setCourseFree(true);
        }
        else {
            periodVo.setCourseFree(false);
        }

        return periodVo;
    }

    @Override
    public List<VideoVo> getPeriodVideoByVideoId(String id) {
        QueryWrapper<Period> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("video_source_id",id);
        Period period = periodMapper.selectOne(videoWrapper);

        QueryWrapper<Period> periodWrapper = new QueryWrapper();
        periodWrapper.eq("course_id",period.getCourseId());
        periodWrapper.eq("is_video",1);
        periodWrapper.orderByDesc("period_sort");
        List<Period> periodList = periodMapper.selectList(periodWrapper);

        //填充vo
        List<VideoVo> videoVoList = new ArrayList<>();
        periodList.forEach(item -> {
            VideoVo videoVo = new VideoVo();
            videoVo.setPeriodId(item.getPeriodId());
            videoVo.setPeriodName(item.getPeriodName());
            videoVo.setSort(item.getSort());
            videoVo.setVideoId(item.getVideoId());
            videoVoList.add(videoVo);
        });

        return videoVoList;
    }
}
